package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import butterknife.BindView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseSelectObject;
import com.isunland.managesystem.entity.CustomerNeedQueryParams;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.widget.SingleLineView;
import java.util.ArrayList;

/**
 * 对象功能: 查询
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/5/25 21:33
 */
public class CustomerNeedQueryFragment extends BaseFragment {
  private static final int REQUEST_DATE = 1;

  @BindView(R.id.slv_customerName) SingleLineView slvCustomerName;
  @BindView(R.id.slv_customerNeed) SingleLineView slvCustomerNeed;
  @BindView(R.id.slv_contactDate) SingleLineView slvContactDate;
  private CustomerNeedQueryParams mQueryParams;

  @Override public void initData() {
    super.initData();
    mQueryParams =
        mBaseParams instanceof CustomerNeedQueryParams ? (CustomerNeedQueryParams) mBaseParams
            : new CustomerNeedQueryParams();
  }

  @Override protected int initLayoutId() {
    return R.layout.fragment_customer_need_query;
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setTitleCustom(R.string.query);
    slvCustomerName.getTvContent().setText(mQueryParams.getCustomerName());
    slvCustomerNeed.getTvContent().setText(mQueryParams.getCustomerNeed());
    //MyDateUtil.getEndofDay(MyDateUtil.getBeforeDay(day)
    slvContactDate.getTvContent().setText(mQueryParams.getDateName());
    //slvContactDate.getTvContent().setText(mQueryParams.getCustomerName());
    slvContactDate.getTvContent().setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        ArrayList<BaseSelectObject> list = new ArrayList<>();
        list.add(new BaseSelectObject("7", "近一周", ""));
        list.add(new BaseSelectObject("30", "近一个月", ""));
        list.add(new BaseSelectObject("90", "近三个月", ""));
        list.add(new BaseSelectObject("365", "近一年", ""));
        list.add(new BaseSelectObject("", "全部", ""));

        showDialog(SimpleArrayDialogFragment.newInstance(list), REQUEST_DATE);
      }
    });
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.menu_item_confirm) {
      Intent intent = new Intent();
      mQueryParams.setCustomerName(slvCustomerName.getTextContent());
      mQueryParams.setCustomerNeed(slvCustomerNeed.getTextContent());
      intent.putExtra(BaseFragment.EXTRA_PARAMS, mQueryParams);
      mActivity.setResult(Activity.RESULT_OK, intent);
      mActivity.finish();
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_DATE) {
      BaseSelectObject result =
          (BaseSelectObject) data.getSerializableExtra(SimpleArrayDialogFragment.EXTRA_VALUE);
      slvContactDate.setTextContent(result.getName());
      mQueryParams.setDateName(result.getName());
      int day = MyStringUtil.valueOfInt(result.getId(), -1);
      //选择了全部
      if (day == -1) {
        mQueryParams.setStartDate("");
        return;
      }
      mQueryParams.setStartDate(
          MyDateUtil.format(MyDateUtil.getEndofDay(MyDateUtil.getBeforeDay(day))));
    }
  }
}

